/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - resource
// Copyright( c) 2014.  All Rights Reserved
//
// File:		AEResource.inl
// Author:		Gianluca Belardelli
// Date:		29/09/2014
//
/////////////////////////////////////////////////////////////////////////
#ifndef _AERESOURCE_INL_
#define _AERESOURCE_INL_

AE_FORCEINLINE const char *AEResource::GetFilename( void ) const
{
#if defined( AE_PLATFORM_ANDROID )
	// do not remove the starting "/" on Android when accessing the data directory
	if( m_lpFilename && (strncasecmp( m_lpFilename, "/data/", 6 ) == 0 ||
		strncasecmp( m_lpFilename, "/storage/", 9 ) == 0 ||
		strncasecmp( m_lpFilename, "/mnt/sdcard/", 12 ) == 0) )

		return m_lpFilename;
#elif defined( AE_PLATFORM_IOS )
	// do not remove the starting "/" on iOS when accessing the var directory
	if( m_lpFilename && (_strnicmp( m_lpFilename, "/var/", 5) == 0) )
		return m_lpFilename;
#endif

    if( m_lpFilename && ( m_lpFilename[0]=='\\' || m_lpFilename[0]=='/'))
    {
		#ifdef AE_PLATFORM_WIN32
			// Decide if filename is UNC-path style filename
			if( m_lpFilename[1] != '\\' )
		#endif
		
		return &m_lpFilename[1];
    }
    
	return m_lpFilename;
}
	
AE_FORCEINLINE const char *AEResource::GetOriginalFilename( void ) const
{
	return m_lpFilename;
}

AE_FORCEINLINE AEINT32 AEResource::GetMemorySize( AEResourceMemoryType eMemoryBits ) const
{
	AEINT32 nSum = 0;

	if( eMemoryBits & AERESOURCEMEMORY_SYSTEM )            nSum += m_uiUniqueSysMemSize;
	if( eMemoryBits & AERESOURCEMEMORY_GPU )               nSum += m_uiUniqueGPUMemSize;
	if( eMemoryBits & AERESOURCEMEMORY_DEPENDENT_SYSTEM )  nSum += m_uiDependentSysMemSize;
	if( eMemoryBits & AERESOURCEMEMORY_DEPENDENT_GPU )     nSum += m_uiDependentGPUMemSize;

	return nSum;
}

AE_FORCEINLINE AEINT32 AEResource::GetMemorySize( void ) const
{
	return m_uiUniqueGPUMemSize + m_uiUniqueSysMemSize + m_uiDependentSysMemSize + m_uiDependentGPUMemSize;
}

AE_FORCEINLINE AEINT32 AEResource::GetGPUMemSize( void ) const
{
	return m_uiUniqueGPUMemSize;
}
	
/// \brief Ritorna il totale in bytes della memoria utilizzata in system RAM.
AE_FORCEINLINE AEINT32 AEResource::GetSysMemorySize( void ) const
{
	return m_uiUniqueSysMemSize;
}
	
//// \brief Returns the amount of system memory used by resources that this resource depends on.
AE_FORCEINLINE AEINT32 AEResource::GetDependentSysMemorySize( void ) const
{
	return m_uiDependentSysMemSize;
}
	
//// \brief Returns the amount of GPU memory used by resources that this resource depends on.
AE_FORCEINLINE AEINT32 AEResource::GetDependentGPUMemorySize( void ) const
{
	return m_uiDependentGPUMemSize;
}

AE_FORCEINLINE void AEResource::SetResourceFlag( AEINT16 nMask )
{
	m_uiResourceFlags |= nMask;
}

AE_FORCEINLINE void AEResource::RemoveResourceFlag( AEINT16 nMask )
{
	m_uiResourceFlags &= (~nMask);
}

AE_FORCEINLINE AEBOOL32 AEResource::IsResourceFlagSet( AEINT16 nMask ) const
{
	return ( m_uiResourceFlags & nMask ) == nMask;
}

AE_FORCEINLINE AEBOOL32 AEResource::IsLoaded( void ) const
{
	return IsResourceFlagSet( AERESOURCEFLAG_ISLOADED );
}

AE_FORCEINLINE AEBOOL32 AEResource::IsLoading( void ) const
{
	return IsResourceFlagSet( AERESOURCEFLAG_ISLOADING );
}
	
AE_FORCEINLINE AEBOOL32 AEResource::IsQueued( void ) const
{
	return IsResourceFlagSet( AERESOURCEFLAG_ISQUEUED );
}
	
AE_FORCEINLINE AEBOOL32 AEResource::IsMissing( void ) const
{
	return IsResourceFlagSet( AERESOURCEFLAG_ISMISSING );
}

AE_FORCEINLINE void AEResource::RemoveMissingFlag( void )
{
	RemoveResourceFlag( AERESOURCEFLAG_ISMISSING );
}

AE_FORCEINLINE void AEResource::FlagAsLoaded( void )
{
	m_fLastTimeUsed = GetGlobalTime();
	SetResourceFlag( AERESOURCEFLAG_ISLOADED );
}

AE_FORCEINLINE void AEResource::FlagAsUnLoaded( void )
{
	RemoveResourceFlag( AERESOURCEFLAG_LOADING_RELATED );
}

AE_FORCEINLINE AEFLOAT32 AEResource::GetLastTimeUsed( void ) const
{
	return m_fLastTimeUsed;
}

AE_FORCEINLINE AEFLOAT32 AEResource::GetLastTimeUsedDiff( void ) const
{
	return GetGlobalTime() - m_fLastTimeUsed;
}

AE_FORCEINLINE void AEResource::AddLock( void )
{
	m_uiLockCounter++;
	EnsureLoaded();
}

AE_FORCEINLINE void AEResource::AddLockNoLoad( void )
{
	m_uiLockCounter++;
}

AE_FORCEINLINE void AEResource::ReleaseLock( void )
{
	AEASSERT( m_uiLockCounter > 0 );
	m_uiLockCounter--;
}

AE_FORCEINLINE AEBOOL32 AEResource::IsLocked( void ) const
{
	return m_uiLockCounter > 0;
}

AE_FORCEINLINE AEResourceManager *AEResource::GetParentManager( void )
{
	return m_lpParentManager;
}

AE_FORCEINLINE void AEResource::EnsureLoaded( void )
{
	m_fLastTimeUsed = GetGlobalTime();
	
	if( IsLoaded() )
		return;
	
	DoReload();
}

AE_FORCEINLINE void AEResource::SetGlobalTime( AEFLOAT32 fTime )
{
	m_fGlobalTime=fTime;
}

AE_FORCEINLINE void AEResource::IncGlobalTime( AEFLOAT32 fDelta )
{
	m_fGlobalTime+=fDelta;
}

AE_FORCEINLINE AEFLOAT32 AEResource::GetGlobalTime( void )
{
	return m_fGlobalTime;
}

#endif // _AERESOURCE_INL_
